package custom.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import common.util.GenarateID;
import common.util.media.ImageUtil;
import common.util.media.VideoUtil;

import dao.ImageInfoDao;
import domain.Resolution;
import entity.ImageInfo;

@Controller

public class MainController extends BaseController {
	@RequestMapping("/chart")
	public ModelAndView getChartDemo(){
		ModelAndView model = new ModelAndView("./../../jsp/widget/charting");
		return model;
	}
	
	@RequestMapping("/grid")
	public ModelAndView getGridDemo(){
		ModelAndView model = new ModelAndView("./../../jsp/widget/grid");
		return model;
	}
	
	@RequestMapping("/form")
	public ModelAndView getFormDemo(){
		ModelAndView model = new ModelAndView("./../../jsp/widget/form");
		return model;
	}
	
	@RequestMapping("/svg")
	public ModelAndView getSvgDemo(){
		ModelAndView model = new ModelAndView("./../../jsp/widget/svg");
		return model;
	}
	
	@RequestMapping("/getUploadImagePage")
	public ModelAndView getUploadImagePage(HttpServletRequest request) throws Exception{
		ModelAndView mav = new ModelAndView("./../../jsp/mobile/uploadImage");
		return mav;
	}
	
	@RequestMapping("/getGalleryPage")
	public ModelAndView getGalleryPage(HttpServletRequest request) throws Exception{
		ModelAndView model = new ModelAndView("./../../jsp/mobile/gallery");
		List<ImageInfo> mediaList = ImageInfoDao.getInstance().getListImageInfo("");
		request.setAttribute("mediaList", mediaList);
		
		return model;
	}
	
	@RequestMapping("/viewVideoDetailPage")
	public ModelAndView viewVideoDetailPage(HttpServletRequest request,
			@RequestParam("id") String id) throws Exception{
		ModelAndView model = new ModelAndView("./../../jsp/mobile/videoDetails");
		String link ="/video.do?fileName=201305101934260.04132085283397624.flv";
		request.setAttribute("link", link);
		
		return model;
	}
	
	@RequestMapping("/demo_sse")
	public @ResponseBody String demo_sse(HttpServletRequest request, HttpServletResponse response) {
		Random r = new Random();
        response.setContentType("text/event-stream");
        try {
                Thread.sleep(10000);
        } catch (InterruptedException e) {
                e.printStackTrace();
        }   
        return "data:Testing 1,2,3" + r.nextInt() +"\n\n";
	}
	
	@RequestMapping("/uploadImage")
	public ModelAndView uploadImage(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("file") MultipartFile file,
			@RequestParam("title") String title,
			@RequestParam("description") String description){
		
		ImageInfo imageInfo = new ImageInfo();
		
		imageInfo.setId(GenarateID.genarate());
		imageInfo.setDateTaken(new Date());
		imageInfo.setFileName(file.getOriginalFilename());
		imageInfo.setTitle(title);
		imageInfo.setFileType("image");
		
		ImageInfoDao imgDao = ImageInfoDao.getInstance();
		imgDao.saveNewImageInfo(imageInfo);
		
		if (imageInfo.getId() == null){
			return json(false,request);
		}
		
		try {
			ImageUtil.saveOriginalFile(file.getInputStream(), imageInfo.getId(),file.getOriginalFilename());
			ImageUtil.createThumbnailImage(file.getInputStream(),imageInfo.getId().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return json(true,request);
	}
	@RequestMapping("/uploadVideo")
	public ModelAndView uploadVideo(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("file") MultipartFile file,
			@RequestParam("title") String title,
			@RequestParam("description") String description){
		
		ImageInfo imageInfo = new ImageInfo();
		
		imageInfo.setId(GenarateID.genarate());
		imageInfo.setDateTaken(new Date());
		imageInfo.setFileName(file.getOriginalFilename());
		imageInfo.setTitle(title);
		imageInfo.setFileType("video");
		
		ImageInfoDao imgDao = ImageInfoDao.getInstance();
		imgDao.saveNewImageInfo(imageInfo);
		
		if (imageInfo.getId() == null){
			return json(false,request);
		}
		
		try {
			String url = VideoUtil.saveOriginalFile(file.getInputStream(), imageInfo.getId(),file.getOriginalFilename());
			VideoUtil.createThumbnailImage(url,imageInfo.getId());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return json(true,request);
	}
}
